package com.leyou.upload.service;

/*
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.leyou.common.exception.domain.ExceptionEnum;
import com.leyou.common.exception.domain.LyException;
import com.leyou.common.uploadimgs.LyConstants;
import com.leyou.upload.config.OSSProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Service
public class UploadService {
    //定义一个图片的map集合
    private static final List<String> ALLOW_IMAGE = Arrays.asList("image/jpg");
    @Autowired
    private OSS client;
    @Autowired
    private OSSProperties prop;

    public String upLoadImages(MultipartFile file) {
        //如果上传的文件不是图片类型怎么办？所以解决上传过滤不是图片的功能！分3部
        //1，得到文件的mime类型
        String mimeType = file.getContentType();

        //2，判断mime类型是否是被允许的类型
        if (!ALLOW_IMAGE.contains(mimeType)) {
            throw new LyException(ExceptionEnum.FILE_UPLOAD_ERROR);

        }
        //3解析问文件 查看是否是真的图片
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(file.getInputStream());
        } catch (IOException e) {
            throw new LyException(ExceptionEnum.FILE_UPLOAD_ERROR);
        }
        if (bufferedImage == null) {
            throw new LyException(ExceptionEnum.FILE_UPLOAD_ERROR);

        }


        //指定文件的路径
        File pathfile = new File(LyConstants.UPLOAD_IMG_PATH);
        //指定文件的名字
        String filename = UUID.randomUUID() + file.getOriginalFilename();


        //上传图片到本地磁盘
        try {
            file.transferTo(new File(pathfile, filename));
        } catch (IOException e) {
            throw new LyException(ExceptionEnum.FILE_UPLOAD_ERROR);

        }
        //上传成功后返回图片的链接地址+图片的名字
        return LyConstants.UPLOAD_IMG_URL + filename;


    }


    public Map<String, Object> getAliSignature() {
        try {
            long expireTime = prop.getExpireTime();
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, prop.getDir());

            String postPolicy = client.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = client.calculatePostSignature(postPolicy);

            Map<String, Object> respMap = new LinkedHashMap<String, Object>();
            respMap.put("accessId", prop.getAccessKeyId());
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", prop.getDir());
            respMap.put("host", prop.getHost());
            respMap.put("expire", expireEndTime);
            return respMap;
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.INVALID_NOTIFY_SIGN);
        }


}
}
*/
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.leyou.common.exception.domain.ExceptionEnum;
import com.leyou.common.exception.domain.LyException;
import com.leyou.common.uploadimgs.LyConstants;
import com.leyou.upload.config.OSSProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Service
public class UploadService {

    private static final List<String> ALLOW_IMAGE = Arrays.asList("image/jpg");

    @Autowired
    private OSS client;

    @Autowired
    private OSSProperties prop;

    /**
     * 本地上传图片业务逻辑
     * @param file
     * @return
     */
    public String localUploadImage(MultipartFile file) {
        //得到当前文件的mime类型
        String mimeType = file.getContentType();
        //判断当前文件的mime类型是否属于允许的类型
        if(!ALLOW_IMAGE.contains(mimeType)){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        //解析文件流，查看是否是真的图片
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(file.getInputStream());
        } catch (IOException e) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        if(bufferedImage==null){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        //指定文件上传的文件夹
        File pathFile = new File(LyConstants.UPLOAD_IMG_PATH);
        //得到文件在文件服务器的名称
        String fileName = UUID.randomUUID()+file.getOriginalFilename();
        //上传图片到本地的主要api
        try {
            file.transferTo(new File(pathFile, fileName));
        } catch (IOException e) {
            throw new LyException(ExceptionEnum.FILE_UPLOAD_ERROR);
        }
        return LyConstants.UPLOAD_IMG_URL+fileName;
    }

    public Map<String, Object> getAliSignature() {
        try {
            long expireTime = prop.getExpireTime();
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, prop.getDir());

            String postPolicy = client.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = client.calculatePostSignature(postPolicy);

            Map<String, Object> respMap = new LinkedHashMap<String, Object>();
            respMap.put("accessId", prop.getAccessKeyId());
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", prop.getDir());
            respMap.put("host", prop.getHost());
            respMap.put("expire", expireEndTime);
            return respMap;
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.INVALID_NOTIFY_SIGN);
        }
    }
}